package com.itheima.mq.rabbitmq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: 基础代码
 * @description: rabbitmq的生产者
 * @author: zhanghz001
 * @create: 2021-05-07 17:39
 **/
@Component
public class RabbitmqProducer {
    // 定义日志记录器
    private static final Logger LOGGER =
            LoggerFactory.getLogger(RabbitmqProducer.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    //不用事务消息,因为会阻塞,一条条加,所以采用的都是publish confirm
    @Transactional(rollbackFor = Exception.class, transactionManager =
            "rabbitTransactionManager")
    public void sendMessage() {
        // 将消息通道设置        为事务机制
        rabbitTemplate.setChannelTransacted(true);
        String msg = "测试生产者事务消息";
        rabbitTemplate.convertAndSend("direct.exchange", "create",
                msg.getBytes());
        // int a = 1 / 0;
        LOGGER.info("transactionManager message send success ----> " +
                msg);
    }
}
